﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using Facebook.Web;

public partial class UploadMultimedia : LutefiskPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Images need to have an absolute path to be displayed!
        // Annoying, I know... but I don't make the rules         
        videoExampleImage.ImageUrl = MasterPage.ConvertRelativeUrlToAbsoluteUrl(Page, "~/Images/uploadvideoexample.png");
        editImage.ImageUrl = MasterPage.ConvertRelativeUrlToAbsoluteUrl(Page, "~/Images/editmode.png");

        // Load current multimedia so the user can view and delete what's currently there.
        LoadMultimedia(CurrentMusician.UID);
    }


    private List<EmbeddedObject> multimediaList;

    protected void MultimediaContentRepeaterPreRender(object sender, System.EventArgs e)
    {
        //check to see if the repeater contains any items and if not hide it
        if (MultimediaContentRepeater.Items.Count == 0)
        {
            MultimediaContentRepeater.Visible = false;
        }
    }


    private void LoadMultimedia(string UID)
    {
        using (SqlConnection dbconn = new SqlConnection(Lutefisk.CONNECTION_STRING))
        {
            dbconn.Open();

            string searchString = "SELECT * FROM musicians WHERE UID=" + UID + ";";
            using (SqlCommand sql = new SqlCommand(searchString, dbconn))
            {
                // Retrieve Videos
                using (SqlDataReader reader = sql.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        multimediaList = EmbeddedObject.CreateList(reader["multimedia"].ToString());

                        // If there are items than bind the multimedia list.
                        if (multimediaList.Count > 1 || (multimediaList.Count == 1 && multimediaList[0].ID != ""))
                        {
                            MultimediaContentRepeater.DataSource = multimediaList;
                            MultimediaContentRepeater.DataBind();
                        }
                    }
                }
            }
        }
    }


    protected void deleteEntryClick(object sender, EventArgs e)
    {
        if (sender is Button)
        {
            Button deleteButton = sender as Button;

            // Get uniqueID of object you're going to be deleting stored in it's command name/arguments
            string uniqueID = deleteButton.CommandName;
            string title = deleteButton.CommandArgument;

            // Construct a semicolon delimited string with the uniqueID passed in removed.
            string newMultimediaString = EmbeddedObject.DeleteEmbeddedObject(multimediaList, uniqueID);

            // if the string is empty then the SQL command should be null
            if (newMultimediaString == "")
            {
                newMultimediaString = "NULL";
            }
            else
            {
                newMultimediaString = "'" + newMultimediaString + "'";
            }

            string sqlInsert = "UPDATE musicians SET multimedia = " + newMultimediaString + " WHERE uid = '" + CurrentMusician.UID + "';";

                //create an SQL connection.
                using (SqlConnection dbconn = new SqlConnection(Lutefisk.CONNECTION_STRING))
                {
                    dbconn.Open();

                    //execute the command
                    using (SqlCommand cmd = new SqlCommand(sqlInsert, dbconn))
                    {
                        try
                        {
                            cmd.ExecuteNonQuery();
                            // After deleting an item show a success dialog box and then refresh the page.
                            Lutefisk.DirectPageAndShowDialog(this, "UploadMultimedia", "Content Deleted", title + " has successfully been deleted.");

                        }
                        catch (SqlException ax)
                        {
                            Console.WriteLine(ax);
                        }
                    }
                }

            
        }
    }


    protected void validateButton_Click(object sender, EventArgs e)
    {
        string embeddedURL = embeddedContentBox.Text;

        if (embeddedURL.StartsWith("http://www.youtube.com/watch?v=") && !embeddedURL.Equals("http://www.youtube.com/watch?v="))
        {
            int idStart = embeddedURL.IndexOf('=') + 1;
            string id = embeddedURL.Substring(idStart,embeddedURL.Length - idStart);

            embeddedContentBox.Text = id;
            bool pageValid = EmbeddedObject.isIDValid(id);

            if (pageValid)
            {
                string newMultimediaString = EmbeddedObject.AddEmbeddedObject(multimediaList, id); 
                string sqlInsert = "UPDATE musicians SET multimedia = '"+newMultimediaString+"' WHERE uid = '" + CurrentMusician.UID + "';";

                //create an SQL connection.
                using (SqlConnection dbconn = new SqlConnection(Lutefisk.CONNECTION_STRING))
                {
                    dbconn.Open();

                    //execute the command
                    using (SqlCommand cmd = new SqlCommand(sqlInsert, dbconn))
                    {
                        try
                        {
                            cmd.ExecuteNonQuery();
                            Lutefisk.DirectPageAndShowDialog(this, "UploadMultimedia", "Link Verified", "The link has been successfully verified and added to your profile.");

                        }
                        catch (SqlException ax)
                        {
                            Console.WriteLine(ax);
                        }
                    }
                }        


            }
            else
            {
                Lutefisk.ShowDialogBox(this, "Link Validation Failed", "The link inputted is not a valid YouTube content page.");
            }

        }
        else
        {
            Lutefisk.ShowDialogBox(this, "Link Validation Failed", "The link inputted is not a valid YouTube content page.");
        }

        embeddedContentBox.Text = "";
    }

    protected void editProfileClick(object sender, EventArgs e)
    {
        Lutefisk.DirectPage(Page.Page, "Form");
    }
    protected void editMultimediaClick(object sender, EventArgs e)
    {
        Lutefisk.DirectPage(Page.Page, "UploadMultimedia");
    }
    protected void editCurriculumVitaeClick(object sender, EventArgs e)
    {
        Lutefisk.DirectPage(Page.Page, "UploadCV");
    }
}